package com.aboverock.autoconfigure.util;

import com.aboverock.core.spring.SpringWebHolder;
import com.aboverock.autoconfigure.properties.JwtProperties;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * JWT工具类.
 * 
 * @author Rock Wang
 *
 */
@Component
@EnableConfigurationProperties(JwtProperties.class)
public class JwtUtil {

    private static String secret;
    private static String header;

    @Autowired
    public JwtUtil(JwtProperties jwtProperties) {
        JwtUtil.secret = jwtProperties.getSecret();
        JwtUtil.header = jwtProperties.getHeader();
    }

    /**
     * 生成jwt token，存储用户名信息.
     *
     * @param loginName the login name
     * @return the string
     */
    public static String generateToken(String loginName) {
        Date nowDate = new Date();

        return Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setSubject(loginName)
                .setIssuedAt(nowDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }

    /**
     * 解析token字符串.
     *
     * @param token the token
     * @return the claim by token
     */
    public static Claims getClaimByToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return null;
        }
        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
    }

    /**
     * 获取请求的token字符串.
     *
     * @param request the request
     * @return the request token
     */
    public static String getRequestToken(HttpServletRequest request) {
        if (null == request) {
            return null;
        }

        // 从header中获取token
        String token = request.getHeader(getHeader());
        // 如果header中不存在token，则从参数中获取token
        if (StringUtils.isEmpty(token)) {
            token = request.getParameter(getHeader());
        }

        return token;
    }

    /**
     * 获取请求的token字符串，并解析.
     *
     * @param request the request
     * @return the claims by http request
     */
    public static Claims getClaimsByHttpRequest(HttpServletRequest request) {
        if (null == request) {
            return null;
        }

        String token = getRequestToken(request);
        return getClaimByToken(token);
    }

    public static String getSecret() {
        return secret;
    }

    public static String getHeader() {
        return header;
    }
    
    
    /**
     * 获取登录名.
     *
     * @return the login name
     */
    public static String getLoginName() {
        Claims claims = JwtUtil.getClaimsByHttpRequest(SpringWebHolder.getRequest());

        if (null != claims) {
            return claims.getSubject();
        }
        return null;
    }

}
